<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        border-bottom: 1px solid #ccc;
        width: 400px;
        position: relative;
      }
      .box input {
        border: 0;
        width: 100%;
        height: 40px;
        outline: none;
      }
      .box img {
        position: absolute;
        top: 12px;
        right: 2px;
        width: 30px;
        height: 30px;
        cursor: pointer;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <input type="password" />
      <img src="./images/open.png" alt="" />
    </div>

    <script>
      //获取元素
      var img = document.querySelector('img');
      var inp = document.querySelector('input');
      // var img = document.querySelector('img')

      //1.事件源
      //2.事件类型
      var ft;
      img.onclick = function () {
        //3事件代码
        if (ft) {
          img.src = './images/close.png';
          ft = false;
          inp.type = 'password';
        } else {
          img.src = './images/open.png';
          ft = true;
          inp.type = 'text';
        }
      };
    </script>
  </body>
</html>
